package com.rcadmin.busi.web.service;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Component;

import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.rcadmin.busi.admin.ddl.SysParam;
import com.rcadmin.busi.web.dao.GroupKeyDao;
import com.rcadmin.busi.web.ddl.WebTemplateGroupkey;
import com.rcadmin.busi.web.ddl.WebTemplatePackage;
import com.rcadmin.busi.web.dto.WebTemplateGroupkeyDto;
import com.rcadmin.core.jpa.BaseDao;
import com.rcadmin.core.jpa.DaoException;
import com.rcadmin.core.jpa.util.PageBean;

/**
 * 分类DAO
 * 
 * @author tanson lam
 * @create 2016年10月18日
 */
@Component
public class GroupKeyService {

	@Resource
	private GroupKeyDao groupKeyDao;
	@Resource
	private BaseDao baseDao;
	@Resource
	private TemplatePackageService templatePackageService;

	/**
	 * 查找单个分类信息
	 * 
	 * @param userId
	 * @param groupKeyId
	 * @return
	 */
	public WebTemplateGroupkey findOne(Integer userId, Integer groupKeyId) {
		return groupKeyDao.findOne(userId, groupKeyId);
	}
	/**
	 * 根据分类编码查询多个分类信息
	 * 
	 * @param groupKeyIds
	 * @return
	 * @throws DaoException
	 */
	public Map<Integer, WebTemplateGroupkey> findByGroupKeyIds(
			List<Integer> groupKeyIds) {
		Map<Integer, WebTemplateGroupkey> map = Maps.newHashMap();
		List<WebTemplateGroupkey> list = groupKeyDao.findByGroupKeyIds(groupKeyIds);
		if (list.isEmpty())
			return map;
		for (WebTemplateGroupkey model : list) {
			map.put(model.getId(), model);
		}
		return map;
	}

	/**
	 * 是否有相同的分类key
	 * 
	 * @param userId
	 * @param groupKeyId
	 * @param groupKey
	 * @return
	 */
	public Boolean isDuplicateKey(Integer userId,Integer groupKeyId, String groupKey) {
		return groupKeyDao.isDuplicateKey(userId, groupKeyId, groupKey);
	}

	/**
	 * 获取所有分组列表，并封装到{@link #SysParam}对象列表返回。
	 */
	public List<SysParam> getGroupParamList(Integer templatePackageId) {
		if (templatePackageId == null)
			return Lists.newArrayList();

		List<WebTemplateGroupkey> grouplist = groupKeyDao
				.findGroupKeyByTemplatePackageId(templatePackageId);
		List<SysParam> sysParamList = new ArrayList<SysParam>();
		for (WebTemplateGroupkey g : grouplist) {
			SysParam p = new SysParam();
			p.setParamValue(g.getGroupKey());
			p.setParamName(g.getName());
			sysParamList.add(p);
		}

		return sysParamList;
	}

	/**
	 * 查询所有分类信息列表，并封装到{@link #SysParam}对象列表返回。
	 *  
	 * @return
	 */
	public List<SysParam> getAllGroupParamList() { 
		List<WebTemplateGroupkey> grouplist = groupKeyDao.findAllEff(); 
		List<SysParam> sysParamList = new ArrayList<SysParam>();
		for (WebTemplateGroupkey g : grouplist) {
			SysParam p = new SysParam();
			p.setParamValue(g.getId().toString());
			p.setParamName(g.getName());
			sysParamList.add(p);
		}

		return sysParamList;
	}
	
	
	public List<SysParam> getGroupParamById(Integer groupKeyId) { 
		 WebTemplateGroupkey  g = baseDao.findOne(groupKeyId, WebTemplateGroupkey.class); 
		List<SysParam> sysParamList = new ArrayList<SysParam>(); 
		if(g==null)
			return sysParamList;
		SysParam p = new SysParam();
		p.setParamValue(g.getId().toString());
		p.setParamName(g.getName());
		sysParamList.add(p); 
		return sysParamList;
	}

	public PageBean<WebTemplateGroupkeyDto> pageQuery(String condition,
			String orderBy, Integer pageSize, Integer page) {
		PageBean<WebTemplateGroupkey> entityPageBean = baseDao.pageQuery(
				condition, orderBy, pageSize, page, WebTemplateGroupkey.class);
		List<WebTemplateGroupkeyDto> dtoList = Lists.newArrayList();
		final List<Integer> idsList = Lists.transform(
				entityPageBean.getResult(),
				new Function<WebTemplateGroupkey, Integer>() {
					@Override
					public Integer apply(WebTemplateGroupkey model) {
						if (model == null)
							return null;
						return model.getTemplatePackageId();
					}
				});

		Map<Integer, WebTemplatePackage> templatePackMap = templatePackageService
				.findByIds(idsList);
		for (WebTemplateGroupkey model : entityPageBean.getResult()) {
			WebTemplateGroupkeyDto dto = new WebTemplateGroupkeyDto();
			try {
				BeanUtils.copyProperties(dto, model);
				if (model.getTemplatePackageId() != null
						&& templatePackMap.get(model.getTemplatePackageId()) != null) {
					dto.setTemplatePackageName(templatePackMap.get(
							model.getTemplatePackageId()).getName());
				}
				dtoList.add(dto);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
		}

		return PageBean.copy2DtoPageBean(entityPageBean, dtoList);
	}
}
